package com.izhengyin.gateway.base.pojo;
import lombok.Data;
import lombok.ToString;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author zhengyin zhengyinit@outlook.com
 * Created on 2020-09-19 11:52
 */
@Data
@ToString
public class Api {

    public static final Api EMPTY;

    static {
        EMPTY = new Api();
        EMPTY.setServer("nil");
        EMPTY.setName("nil");
        EMPTY.setMappingInfo("nil");
        EMPTY.setMethod("nil");
        EMPTY.setHttpMethod(new HashSet<>());
        EMPTY.setPathPatterns(new HashSet<>());
    }

    /**
     * 服务名
     */
    private String server;
    /**
     * 接口名
     */
    private String name;
    /**
     * 处理接口请求的method
     */
    private String method;
    /**
     * http method (可以多个)
     */
    private Set<String> httpMethod;
    /**
     * 路径规则 (可以多个)
     */
    private Set<String> pathPatterns;
    /**
     * spring mapping 描述
     */
    private String mappingInfo;
    /**
     * 请求数量（粗略值，用于近似排序）
     */
    private int requestNum;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Api api = (Api) o;
        return requestNum == api.requestNum && Objects.equals(server, api.server) && Objects.equals(name, api.name) && Objects.equals(method, api.method) && Objects.equals(httpMethod, api.httpMethod) && Objects.equals(pathPatterns, api.pathPatterns) && Objects.equals(mappingInfo, api.mappingInfo);
    }

    @Override
    public int hashCode() {
        return Objects.hash(server, name, method, httpMethod, pathPatterns, mappingInfo, requestNum);
    }
}
